<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
								xmlns:p="http://www.example.org/products"
								xmlns:validationRule="xalan://com.epam.productxslt.validation.ValidationRule"
								xmlns:validationResult="xalan://com.epam.productxslt.validation.ValidationResult"
								xmlns:map = "xalan://java.util.HashMap"
								xmlns:list = "xalan://java.util.ArrayList"
								xmlns:validator = "xalan://com.epam.productxslt.validation.validator.FormValidator"
								extension-element-prefixes="map list validator" 
								exclude-result-prefixes = "p">
	
	<xsl:import href="addGoods.xsl"/>
	<xsl:param name="categoryName" />
	<xsl:param name="subcategoryName" />
	<xsl:param name="model" />
	<xsl:param name="producer" />
	<xsl:param name="color" />
	<xsl:param name="date" />
	<xsl:param name="price" />
	<xsl:param name="notInStock" select="false()"/>
	<xsl:param name="validationResult" />
	<xsl:param name="validationRules" />
	<xsl:param name="formValidator" />
	<xsl:variable name="namespace" select="'http://www.example.org/products'" />
	
	<xsl:template match="/">
		<xsl:variable name="isValidProducer" select="validator:validateInput($formValidator,'producer', $producer)" />
		<xsl:variable name="isValidModel" select="validator:validateInput($formValidator,'model', $model)" />
		<xsl:variable name="isValidDate" select="validator:validateInput($formValidator,'date', $date)" />
		<xsl:variable name="isValidColor" select="validator:validateInput($formValidator,'color', $color)" />
		<xsl:variable name="isValidPrice">
			<xsl:choose>
				<xsl:when test="not($notInStock)">
					<xsl:value-of select="validator:validateInput($formValidator,'price', $price)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="true()" />
				</xsl:otherwise>
			</xsl:choose>			
		</xsl:variable>
		
		<xsl:choose>
			<xsl:when test="not(validator:isValidForm($formValidator))">
				<xsl:call-template name="add-goods">
					<xsl:with-param name="producer" select="$producer" />
					<xsl:with-param name="model" select="$model" />
					<xsl:with-param name="color" select="$color" />
					<xsl:with-param name="date" select="$date" />
					<xsl:with-param name="price" select="$price" />
					<xsl:with-param name="errors" select="validator:getErrorMessages($formValidator)" />
				</xsl:call-template>	
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy/>
				<xsl:call-template name="saveGood" />
			</xsl:otherwise>
		</xsl:choose>		
	</xsl:template>	
	
	<xsl:template match="@*|node()" name="saveGood">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
	
	<xsl:template match="/p:products/p:category[@name=$categoryName]/p:sub-category[@name=$subcategoryName]">
		<xsl:copy>
            <xsl:apply-templates select="@* |*" />     	
           	<xsl:element name="good" namespace="{$namespace}">
                <xsl:element name="producer" namespace="{$namespace}">
                    <xsl:value-of select="$producer" />
                </xsl:element>
                <xsl:element name="model" namespace="{$namespace}">
                    <xsl:value-of select="$model" />
                </xsl:element>
                <xsl:element name="date-of-issue" namespace="{$namespace}">
                    <xsl:value-of select="$date" />
                </xsl:element>
                <xsl:element name="color" namespace="{$namespace}">
                    <xsl:value-of select="$color" />
                </xsl:element>
                <xsl:choose>
                    <xsl:when test="$notInStock">
                    	<xsl:element name="not-in-stock" namespace="{$namespace}"></xsl:element>                        
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="price" namespace="{$namespace}">
                            <xsl:value-of select="$price" />
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:copy>
        
	</xsl:template>
	
</xsl:stylesheet>